#include <stdio.h>
#include <graphics.h>
#include <conio.h>
#include <time.h>

int height, width, block, space;
int a[10];
int i, j;

void givenumber()
{
	height = 600;
	width = 775;
	block = 50;
	space = 25;
	srand(time(NULL));
	for (int m = 0; m < 10; m++)
		a[m] = rand() % 40 +10;
}

void draw()
{
	cleardevice();
	
	BeginBatchDraw();
	for (int n = 0; n < 10; n++)
	{
		setfillcolor(WHITE);
		if (n == i)
			setfillcolor(RGB(255, 0, 0));
		if (n == j)
			setfillcolor(RGB(0, 0, 255));
		fillrectangle(space + n * (space + block), height - 10 * a[n], (n + 1) * (space + block), height);

		TCHAR s[20];
		swprintf_s(s, _T("%d"), a[n]);
		settextstyle(50, 0, _T("Times"));
		settextcolor(WHITE);
		outtextxy(3+space + n * (space + block), height - 10 * a[n]-50, s);

		if (i == 10)
		{
			TCHAR word1[100] = _T("按空格键来重新开始");
			TCHAR word2[100] = _T("按E停止程序");
			settextstyle(50, 0, _T("Times"));
			settextcolor(WHITE);
			outtextxy(0, 0, word1);
			outtextxy(0, 50, word2);
		}

		FlushBatchDraw();
	}
	Sleep(1000);
}

int main()
{
	there:
	givenumber();
	initgraph(width, height);
	draw();

	for (i = 0; i < 10; i++)
	{
		for (j = i + 1; j < 10; j++)
		{
			int temp;
			if (a[i] > a[j])
			{
				temp = a[i];
				a[i] = a[j];
				a[j] = temp;
			}
			draw();
		}
	}
	draw();

	while (1)
	{
		char answer = _getch();
		if (answer == ' ')
			goto there;
		if (answer == 'e')
			return 0;
	}
}